package com.aixin.csopuser.repository;

import com.aixin.csopuser.domain.ServiceConsumeHistory;
import com.aixin.csopuser.model.ServicesHistory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

public interface ServicesHistoryRepository extends JpaRepository<ServiceConsumeHistory,Long>, JpaSpecificationExecutor<ServiceConsumeHistory> {

    @Query(value = "select historyNumber,customerID,consumeTime,remarks,numConsumed from serviceconsumehistory  WHERE customerID = :customerID and equityID = :equityID and equityLevelID = :equityLevelID " +
            " and ( supplierID is null or supplierID = :supplierID) ", nativeQuery = true)
    List<Map<String,String>> getServicesHistory(@Param("customerID") String  customerID, @Param("equityID") String  equityID, @Param("equityLevelID") String  equityLevelID, @Param("supplierID") String  supplierID);

    @Query(value = "select * from serviceconsumehistory where  historyNumber = :historyNumber " ,nativeQuery = true)
    ServiceConsumeHistory  getConsumedByHistoryNumber(@Param("historyNumber") String historyNumber);


    @Modifying
    @Query(value ="UPDATE  serviceconsumehistory  SET serviceID = :serviceID,consumeTime = :consumeTime, remarks= :remarks,"
            + "numConsumed = :numConsumed where historyNumber =:historyNumber" ,nativeQuery = true)
    int updateConsumeHistory(@Param("historyNumber") String historyNumber,@Param("consumeTime") String consumeTime,@Param("remarks") String remarks,@Param("numConsumed") int numConsumed,@Param("serviceID") String serviceID);

    @Modifying
    @Query(value ="UPDATE  supplierdef SET totalOfService = totalOfService + :totalOfService,"
            + "updateTime = :updateTime where supplierID =:supplierID" ,nativeQuery = true)
    int updateSupplierDef(@Param("totalOfService") int totalOfService,@Param("updateTime") String updateTime,@Param("supplierID") String supplierID);

    @Modifying
    @Query(value ="UPDATE  servicedef  SET totalNumber = totalNumber + :totalNumber ,"
            + "updateTime = :updateTime where serviceID =:serviceID" ,nativeQuery = true)
    int updateServiceDef(@Param("totalNumber") int totalNumber,@Param("updateTime") String updateTime,@Param("serviceID") String serviceID);

    @Modifying
    @Query(value ="UPDATE  customerownedservices  SET usedNum = usedNum + :usedNum ,useTime =:useTime  "
            + " where custServiceNum =:custServiceNum" ,nativeQuery = true)
    int updateCustOwnedservice(@Param("usedNum") int usedNum,@Param("custServiceNum") String custServiceNum,@Param("useTime") String useTime);

    List<ServiceConsumeHistory> findAllByCustomerIDAndEquityIDAndEquityLevelID(String customerID, String equityID, String equityLevelID);
}
